
import couponApi from '../../api/coupon'
import * as types from '../../constants/ActionTypes'
import { message } from 'antd';

//优惠券列表
const getCouponList = (couponList,couponListTotal) => ({
    type: types.GET_COUPON_LIST,
    couponList,
    couponListTotal
})

export const checkCouponData = (data) => dispatch => {        
    return couponApi.getCouponData(data,res => {
        dispatch(getCouponList(res.recordList,res.totalCount));
    })
    
}

//优惠券信息
const getCouponInfo = (couponInfo,id) => ({
    type: types.GET_COUPON_INFO,
    couponInfo,
    id
})

export const checkCouponInfo = (data) => dispatch => {     
    return couponApi.getCouponInfo(data,res => {
        dispatch(getCouponInfo(res,data.id));
    })    
} 

//业务层次下拉框
const getBusinessLevelSelect = (businessLevelSelect) => ({
    type: types.GET_BUSINESS_LEVEL_SELECT,
    businessLevelSelect,
})

export const checkBusinessLevelSelect = (data) => dispatch => {     
    return couponApi.getBusinessLevelSelect(data,res => {
        dispatch(getBusinessLevelSelect(res));
    })    
}

// 编辑优惠券
const changeEditCouponStatus = (editCouponStatus) => ({
    type: types.EDIT_COUPON,
    editCouponStatus,    
})


export const editCoupon = (data) => dispatch => {         
    couponApi.getEditCouponStatus(data,res => {
        dispatch(changeEditCouponStatus(res)); 
        if(res){
            //编辑成功
            message.success('编辑成功');                 
            return couponApi.getCouponData(data.searchData,res => {
                dispatch(getCouponList(res.recordList,res.totalCount));
            })   
        }               
    })    
}

//获取组织数据
const getOrgList = (orgList) => ({
    type: types.GET_ORG_LIST,
    orgList,
})

export const checkOrgList = (data) => dispatch => {     
    return couponApi.getOrgList(data,res => {
        dispatch(getOrgList(res));
    })    
}

//获取优惠券对应的产品信息
const getCouponProductList = (couponProductList) => ({
    type: types.GET_COUPON_PRODUCT_LIST,
    couponProductList,
})

export const checkCouponProductList = (data) => dispatch => {     
    return couponApi.getCouponProductList(data,res => {
        dispatch(getCouponProductList(res));
    })    
}
